package com.matrix.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author liuweizhong
 * @since 2025-09-12 08:45
 */
@Slf4j
public class JSONUtils {

    /**
     * 默认日期格式
     */
    private static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.setDateFormat(DEFAULT_DATE_FORMAT);
    }

    private JSONUtils() {
    }

    /**
     * 将对象转换为json字符串
     *
     * @param obj 对象
     * @return json字符串
     */
    public static String toJSONString(Object obj) {
        String jsonStr;
        try {
            jsonStr = mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("============[object to json string error]============: {}", e.getMessage());
            return null;
        }
        return jsonStr;
    }

    /**
     * 将对象转换为json字符串
     *
     * @param obj        对象
     * @param dateFormat 日期格式
     * @return json字符串
     */
    public static String toJSONString(Object obj, String dateFormat) {
        String jsonStr;
        try {
            mapper.setDateFormat(new SimpleDateFormat(dateFormat));
            jsonStr = mapper.writeValueAsString(obj);
        } catch (Exception e) {
            log.error("============[object to json string error]============: {}", e.getMessage());
            return null;
        }
        mapper.setDateFormat(DEFAULT_DATE_FORMAT);
        return jsonStr;
    }

    /**
     * 将json字符串转换为对象
     *
     * @param jsonStr json字符串
     * @param clazz   对象类型
     * @param <T>     对象类型
     * @return 对象
     */
    public static <T> T parseObject(String jsonStr, Class<T> clazz) {
        T obj;
        try {
            obj = mapper.readValue(jsonStr, clazz);
        } catch (JsonProcessingException e) {
            log.error("============[json string to object error]============: {}", e.getMessage());
            return null;
        }
        return obj;
    }

    /**
     * 将json字符串转换为对象
     *
     * @param jsonStr json字符串
     * @param clazz   对象类型
     * @param <T>     对象类型
     * @return 对象
     */
    public static <T> T parseObject(String jsonStr, String dateFormat, Class<T> clazz) {
        T obj;
        try {
            mapper.setDateFormat(new SimpleDateFormat(dateFormat));
            obj = mapper.readValue(jsonStr, clazz);
        } catch (Exception e) {
            log.error("============[json string to object error]============: {}", e.getMessage());
            return null;
        }
        mapper.setDateFormat(DEFAULT_DATE_FORMAT);
        return obj;
    }

    /**
     * 将json字符串转换为对象列表
     *
     * @param jsonStr json字符串
     * @return 对象列表
     */
    public static <T> List<T> parseArray(String jsonStr, Class<T> clazz) {
        List<T> list;
        try {
            list = mapper.readValue(jsonStr, new TypeReference<>() {
            });
        } catch (JsonProcessingException e) {
            log.error("============[json string to object list error]============: {}", e.getMessage());
            return Collections.emptyList();
        }
        return list;
    }

    /**
     * 将json字符串转换为对象列表
     *
     * @param jsonStr json字符串
     * @return 对象列表
     */
    public static <T> List<T> parseArray(String jsonStr, String dateFormat, Class<T> clazz) {
        List<T> list;
        try {
            mapper.setDateFormat(new SimpleDateFormat(dateFormat));
            list = mapper.readValue(jsonStr, new TypeReference<>() {
            });
        } catch (JsonProcessingException e) {
            log.error("============[json string to object list error]============: {}", e.getMessage());
            return Collections.emptyList();
        }
        mapper.setDateFormat(DEFAULT_DATE_FORMAT);
        return list;
    }

    /**
     * 将json字符串转换为对象map
     *
     * @param jsonStr json字符串
     * @return 对象map
     */
    public static Map<String, Object> parseMap(String jsonStr) {
        Map<String, Object> map;
        try {
            map = mapper.readValue(jsonStr, new TypeReference<>() {
            });
        } catch (JsonProcessingException e) {
            log.error("============[json string to object map error]=: {}", e.getMessage());
            return null;
        }
        return map;
    }

    /**
     * 将json字符串转换为对象map
     *
     * @param jsonStr json字符串
     * @return 对象map
     */
    public static Map<String, Object> parseMap(String jsonStr, String dateFormat) {
        Map<String, Object> map;
        try {
            mapper.setDateFormat(new SimpleDateFormat(dateFormat));
            map = mapper.readValue(jsonStr, new TypeReference<>() {
            });
        } catch (JsonProcessingException e) {
            log.error("============[json string to object map error]=: {}", e.getMessage());
            return null;
        }
        mapper.setDateFormat(DEFAULT_DATE_FORMAT);
        return map;
    }

    /**
     * 将json字符串转换为对象map
     *
     * @param jsonStr json字符串
     * @return 对象map
     */
    public static List<Map<String, Object>> parseArrayMap(String jsonStr) {
        List<Map<String, Object>> listMap;
        try {
            listMap = mapper.readValue(jsonStr, new TypeReference<>() {
            });
        } catch (JsonProcessingException e) {
            log.error("============[json string to object list map error]=: {}", e.getMessage());
            return Collections.emptyList();
        }
        return listMap;
    }

    /**
     * 将json字符串转换为对象map
     *
     * @param jsonStr json字符串
     * @return 对象map
     */
    public static List<Map<String, Object>> parseArrayMap(String jsonStr, String dateFormat) {
        List<Map<String, Object>> listMap;
        try {
            mapper.setDateFormat(new SimpleDateFormat(dateFormat));
            listMap = mapper.readValue(jsonStr, new TypeReference<>() {
            });
        } catch (JsonProcessingException e) {
            log.error("============[json string to object list map error]=: {}", e.getMessage());
            return Collections.emptyList();
        }
        mapper.setDateFormat(DEFAULT_DATE_FORMAT);
        return listMap;
    }

}